Method of Android Programming to Realize Recording Saving and Playing Function [Attached with demo Source Code Download]
- 2021-08-12 03:36:08
- OfStack
This paper describes the method of realizing recording, saving and playing functions by Android programming. Share it for your reference, as follows:
It is relatively simple to record in android. Use the MediaRecorder class provided by the system to record and save, and then call MediaPlayer to play. The following is the xml profile code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.kk.soundrecording.MainActivity" >
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="@string/start" />
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/start"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="@string/stop" />
<Button
android:id="@+id/paly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/stop"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="@string/paly" />
<Button
android:id="@+id/pause_paly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/paly"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="@string/pause_paly" />
<Button
android:id="@+id/stop_paly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/pause_paly"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="@string/stop_paly" />
</RelativeLayout>
Record in MainActivity with the following code:
package com.example.kk.soundrecording;
import java.io.File;
import java.io.IOException;
import com.example.kk.util.RecordPlayer;
import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
*
* @author kk
*
*/
public class MainActivity extends Activity implements OnClickListener {
// Start recording
private Button start;
// Stop button
private Button stop;
// Play button
private Button paly;
// Pause playback
private Button pause_paly;
// Stop playing
private Button stop_paly;
// Recording class
private MediaRecorder mediaRecorder;
// Save as a file
private File recordFile;
private RecordPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recordFile = new File("/mnt/sdcard", "kk.amr");
initView();
Listener();
}
private void initView() {
start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
paly = (Button) findViewById(R.id.paly);
pause_paly = (Button) findViewById(R.id.pause_paly);
stop_paly = (Button) findViewById(R.id.stop_paly);
}
private void Listener() {
start.setOnClickListener(this);
stop.setOnClickListener(this);
paly.setOnClickListener(this);
pause_paly.setOnClickListener(this);
stop_paly.setOnClickListener(this);
}
@Override
public void onClick(View v) {
player = new RecordPlayer(MainActivity.this);
int Id = v.getId();
switch (Id) {
case R.id.start:
startRecording();
break;
case R.id.stop:
stopRecording();
break;
case R.id.paly:
playRecording();
break;
case R.id.pause_paly:
pauseplayer();
break;
case R.id.stop_paly:
stopplayer();
break;
}
}
private void startRecording() {
mediaRecorder = new MediaRecorder();
// If the current file already exists, delete it
if (recordFile.exists()) {
recordFile.delete();
}
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mediaRecorder.setOutputFile(recordFile.getAbsolutePath());
try {
// Get ready to start recording
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void stopRecording() {
if (recordFile != null) {
mediaRecorder.stop();
mediaRecorder.release();
}
}
private void playRecording() {
player.playRecordFile(recordFile);
}
private void pauseplayer() {
player.pausePalyer();
}
private void stopplayer() {
// TODO Auto-generated method stub
player.stopPalyer();
}
}
At the same time, create a new RecordPlayer class to play the saved recording, as follows:
package com.example.kk.util;
import java.io.File;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;
import com.example.kk.soundrecording.R;
/**
*
*
* @author kk Recording playback class
*
*/
public class RecordPlayer {
private static MediaPlayer mediaPlayer;
private Context mcontext;
public RecordPlayer(Context context) {
this.mcontext = context;
}
// Play a recording file
public void playRecordFile(File file) {
if (file.exists() && file != null) {
if (mediaPlayer == null) {
Uri uri = Uri.fromFile(file);
mediaPlayer = MediaPlayer.create(mcontext, uri);
}
mediaPlayer.start();
// Eavesdropping MediaPlayer Play complete
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer paramMediaPlayer) {
// TODO Auto-generated method stub
// Pop-up prompt
Toast.makeText(mcontext,
mcontext.getResources().getString(R.string.ok),
Toast.LENGTH_SHORT).show();
}
});
}
}
// Pause playback of recording
public void pausePalyer() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
Log.e("TAG", " Pause playback ");
}
}
// Stop playing recording
public void stopPalyer() {
// Is not called here stop() That calls the seekto(0), Restore the playback progress to the beginning
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
mediaPlayer.seekTo(0);
Log.e("TAG", " Stop playing ");
}
}
}
At this point, the functional code has been implemented, but the runtime will report an error! Why? This is forgotten by many beginners, that is, when calling the corresponding functions in android development, the corresponding permissions must be granted in the main configuration file, and the following code must be added to the configuration file:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
In addition, for a detailed description of Android permission control, please refer to the Android Manifest Function and Permission Description Encyclopedia
Attachment: Demo source code click here to download this site.
For more readers interested in Android related content, please check the topics on this site: Introduction and Advanced Tutorial of Android Development, Summary of Android Multimedia Operation Skills (Audio, Video, Recording, etc.), Summary of View Skills in Android View, Summary of activity Operation Skills in Android Programming, Summary of Android Operation Skills in json Format Data, Summary of Android Resource Operation Skills and Summary of Android Control Usage
I hope this article is helpful to everyone's Android programming.